package com.itheima.activiti.controller.modeler;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.activiti.common.R;
import com.itheima.activiti.dto.modeler.ModelDTO;
import com.itheima.activiti.service.modeler.ModelerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/modeler")
@Api(tags = "模型管理")
public class ModelerController {

    @Autowired
    private ModelerService modelerService;

    /**
     * 新增模型
     */
    @PostMapping
    @ApiOperation(value = "新增")
    public R<ModelDTO> save(@RequestBody ModelDTO modelDTO) {
        log.info("modelDTO:{}", modelDTO);
        String id = modelerService.save(modelDTO.getName(),
                modelDTO.getKey(), modelDTO.getDescription());
        modelDTO.setId(id);
        return R.success(modelDTO);
    }

    /**
     * 保存模型流程图
     */
    @PostMapping("xml")
    @ApiOperation(value = "保存流程图内容")
    public R<ModelDTO> saveXml(@RequestBody ModelDTO modelDTO) {
        log.info("ModelXmlDTO:{}", modelDTO);
        modelerService.saveXml(modelDTO);
        return R.success(modelDTO);
    }

    /**
     * 删除模型
     */
    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@RequestParam String id) {
        modelerService.delete(id);
        return R.success("删除成功");
    }

    /**
     * 查询流程图文件
     */
    @GetMapping("/xml/{id}")
    @ApiOperation(value = "查询流程图文件")
    public R getXml(@PathVariable String id) {
        String content = modelerService.getXmlById(id);
        return R.success(content);
    }

    /**
     * 分页查询
     * 搜索查询
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页")
    public R<Page<ModelDTO>> page(ModelDTO modelDTO,
                                  @RequestParam(required = false, defaultValue = "1") int page,
                                  @RequestParam(required = false, defaultValue = "10") int pageSize) {
        log.info("page:{}  pageSize:{}  model:{}", page, pageSize, modelDTO);

        Page<ModelDTO> pageResult = modelerService.page(modelDTO, page, pageSize);
        //返回结果
        return R.success(pageResult);
    }
}
